﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WayPointAI : MonoBehaviour
{

    
    public List<WayPoint> m_WaypointList = new List<WayPoint>();
    private int m_iWayPointIdx = 0;
    
    private GameObject m_ObjActer;
    private bool m_bIsAiEnd = false;

    int m_iLog_Cur = 0;

    void Start()
    {
        m_ObjActer = transform.parent.GetChild(0).gameObject;
    }

    void Update()
    {
        if (m_WaypointList.Count != 0 || 
            m_bIsAiEnd == false)
        {
            if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().WayPointEndCheck(m_ObjActer.transform.position))
            {
                if ((m_iWayPointIdx + 1) != (m_WaypointList.Count))
                {
                    m_iWayPointIdx += 1;
                    m_iLog_Cur = m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_iLog_Start;
                }
                else
                    m_bIsAiEnd = true;
            }
            else
            {
                if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ActID == WAYPOINTACT_ID.ACT_MOVE)
                {
                    m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_bPlayTimer = true;
                    //m_ObjActer.GetComponent<PolyNavAgent>().SetDestination(m_WaypointList[m_iWayPointIdx].transform.position);
                    m_ObjActer.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);
                }
                else if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ActID == WAYPOINTACT_ID.ACT_CHANGEANIM)
                {
                    m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_bPlayTimer = true;
                    m_ObjActer.GetComponent<Animator>().SetInteger("iAnimID", (int)m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ChangeAnimID);
                    if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle != -1.0f)
                        m_ObjActer.transform.rotation =  Calculate.RotToAngle(m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle);

                }
                else if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ActID == WAYPOINTACT_ID.ACT_CHAT)
                {
                    if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_iLog_End >= m_iLog_Cur)
                    {
                        if (!m_ObjActer.GetComponent<UnitStatus>().m_unitData.bTalking)
                        {
                            m_ObjActer.GetComponent<PoliceFSM>().Chat(CHATBUBBLE_STATE.EVENT, m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_iSceneID, m_iLog_Cur);
                            m_iLog_Cur += 1;
                        }
                    }
                    else
                        m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_bPlayTimer = true;

                    m_ObjActer.GetComponent<Animator>().SetInteger("iAnimID", (int)m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ChangeAnimID);
                    if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle != -1.0f)
                        m_ObjActer.transform.rotation = Calculate.RotToAngle(m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle);
                }
                else if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_ActID == WAYPOINTACT_ID.ACT_STANDBY)
                {
                    m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_bPlayTimer = true;
                    if (m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle != -1.0f)
                        m_ObjActer.transform.rotation = Calculate.RotToAngle(m_WaypointList[m_iWayPointIdx].GetComponent<WayPoint>().m_fChangeAngle);
                }

                
            }
        }

        

    }
}
